package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseSaleAttr;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SpuSaveVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "SPU管理")
@RestController
@RequestMapping("/admin/product")
public class SpuController {
    /**
     * 获取spu分页列表
     *
     * @param page
     * @param limit
     * @return
     */
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    BaseSaleAttrService baseSaleAttrService;
    @Autowired
    SpuImageService  spuImageService;

    /**
     * 400: 请求参数不对。
     *
     * @param category3Id
     * @param pn
     * @param ps
     * @return
     */

    @GetMapping("/{page}/{limit}")
    public Result getSpuPageLimit(@PathVariable("page") Long page,
                                  @PathVariable("limit") Long limit,
                                  @RequestParam("category3Id") Long category3Id) {

        Page<SpuInfo> pages = spuInfoService.lambdaQuery()
                .eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<SpuInfo>(page, limit));
        return Result.ok(pages);
    }

    //TODO   http://api.gmall.com/admin/product/baseSaleAttrList 无参数
    //返回参数  是 id 和 name
    @ApiOperation("销售属性管理 品牌列表")
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList() {
        //从数据库表中的 base_sale_attr中去查
        List<BaseSaleAttr> baseSaleAttrs = baseSaleAttrService.list();
        return Result.ok(baseSaleAttrs);
    }

    /**
     *  添加 SPU
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo (@RequestBody SpuSaveVo spuSaveVo){
      spuInfoService.saveSpuInfo(spuSaveVo);
     return  Result.ok();
    }


}


